﻿using System.Linq.Expressions;


namespace SBlogCore.Common.DatabaseAccessor.Repositorys.EFCore
{
    public partial interface IRepository<TEntity> where TEntity : class, IBaseEntity
    {
        #region 查询
        /// <summary>
        /// 查询第一个
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        TEntity? FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
        /// <summary>
        /// 异步查询第一个
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// 异步查询第一个
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// 查询第一个
        /// </summary>
        /// <returns></returns>
        TEntity? FirstOrDefault();

        /// <summary>
        /// 异步查询第一个
        /// </summary>
        /// <returns></returns>
        Task<TEntity?> FirstOrDefaultAsync();
        /// <summary>
        /// Id查找数据
        /// </summary>
        /// Find会优先查询缓存
        /// <returns></returns>
        TEntity? FindById(long id);
        /// <summary>
        /// 异步Id查找数据 
        /// </summary>
        /// Find会优先查询缓存
        /// <returns></returns>
        ValueTask<TEntity?> FindByIdAsync(long id);
        /// <summary>
        /// 查看是否存在
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
        /// <summary>
        /// 查看是否存在
        /// </summary>
        /// <param name="predicate"></param>    
        /// <returns></returns>
        bool Any(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// 使用SQL脚本查询
        /// </summary>
        /// <typeparam name="T"> T为非数据库实体，需要在DbContext中增加对应的DbQuery</typeparam>
        /// <returns></returns>
        IQueryable<TEntity> Query<T>(string sql, params object[] parameters);

        IQueryable<TEntity> Query<T>(int pageindex, int pageSize, string orderBy, string sql, ref int count, params object[] parameters) where T : class;
        #endregion
    }
}